package cn.smbms.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.smbms.dao.RoleMapper;
import cn.smbms.dao.UserMapper;
import cn.smbms.pojo.User;
import cn.smbms.service.UserService;

//@Service userServiceImpl
@Service("userService")
//@Transactional(rollbackFor=Exception.class)
public class UserServiceImpl implements UserService {

	@Resource(name="userMapper")
	private UserMapper userMapper;
	
	@Resource(name="roleMapper")
	private RoleMapper roleMapper;
	
	public int count() {
		return userMapper.count();
	}

	/**
	 * service 存在的意义
	 * 1、一个服务接口同时调用多个 dao 接口，生成订单的服务接口（订单表、购物车、库存表、用户表）
	 * 2、不仅调用多个接口，更重要的是保持数据库中表数据的一致性（事务）
	 */
	
	public int add(User user) {
		return userMapper.add(user);
	}

	
	public List<User> getUserList() {
		return userMapper.getUserList();
	}

	
	@Transactional
	public int modify(User user) {
		return userMapper.modify(user);
	}

	
	public User login(String userCode, String userPassword) {
		// 验证用户名是否存在
		User user = userMapper.getLoginUser(userCode);
		// 验证用户密码是否正确
		if(user!=null &&
				user.getUserPassword().equals(userPassword)) {
			return user;
		}
		return null;
	}

	
	public int updatePwd(Integer id, String pwd) {
		return userMapper.updatePwd(id, pwd);
	}

	
	public User getUserById(Integer userid) {
		return userMapper.getUserById(userid);
	}

	
	public int deleteById(Integer userid) {
		return userMapper.deleteUserById(userid);
	}

	public List<User> getUserListByUserName(String queryname) {
		//
		return userMapper.getUserListByUserName(queryname);
	}

	public List<User> getUserLists(User user) {
		//
		return userMapper.getUserLists(user);
	}

	@Override
	public User getUserByUserName(String username) {
		//
		return userMapper.getUserByUserName(username);
	}

}
